<template>
  <div>
    <div class="flexsb flexcc pl16 pr16">
      <div class="fz16">
        <span class="mr6">全部课程</span
        ><span style="color: #999999">{{ data.length }}</span>
      </div>
      <van-dropdown-menu>
        <van-dropdown-item
          v-model="value"
          :options="option"
          @change="handleChange"
        />
      </van-dropdown-menu>
    </div>
    <van-list
      v-model="isUpLoading"
      :finished="upFinished"
      :immediate-check="false"
      :offset="10"
      finished-text="数据已全部加载完毕~"
      @load="onLoadList"
    >
      <video-card
        v-for="item in data"
        :key="item.id"
        :item="item"
        @handleClick="handleClick"
      />
    </van-list>
  </div>
</template>
<script>
import API from "@/services";
import VideoCard from "@/components/video-card-farmer";
import { Bus } from "@/utils/eventBus";
export default {
  name: "FarmerVideoList",
  data() {
    return {
      data: [],
      name: "",
      type: 0,
      value: 0,
      option: [
        { text: "默认排序", value: 0 },
        { text: "最新发布", value: 1 },
        { text: "最多播放", value: 2 },
      ],
      isUpLoading: false, // 上拉加载
      upFinished: false, // 上拉加载完毕
      offset: 100, // 滚动条与底部距离小于 offset 时触发load事件
      pageSize: 10, // 每页条数
      pageIndex: 1, // 页码
    };
  },
  async created() {
    await this.getList();
    Bus.$on("searchConfirm", (val) => {
      this.name = val;
      this.pageIndex = 1;
      this.data = [];
      this.upFinished = false;
      this.isUpLoading = false;
      this.getList();
    });
  },
  beforeDestroy() {
    Bus.$off("searchConfirm");
  },
  methods: {
    onLoadList() {
      this.pageIndex++;
      this.getList();
    },
    async getList() {
      const res = await API.getFarmerCourseList({
        params: {
          name: this.name,
          type: this.type,
          pageSize: 10,
          currentPage: this.pageIndex,
        },
      });
      if (res.list == null || res.list.length === 0) {
        this.upFinished = true;
        return;
      }

      if (res.list.length < this.pageSize) {
        this.upFinished = true;
      }

      if (this.pageIndex === 1) {
        this.data = res.list;
      } else {
        this.data = this.data.concat(res.list);
      }
    },
    handleClick(val) {
      this.$router.push({ name: "VideoCourse", params: { courseId: val } });
    },
    handleChange(val) {
      this.type = val;
      this.pageIndex = 1;
      //   this.data = [];
      this.upFinished = false;
      this.isUpLoading = false;
      this.getList();
    },
  },
  components: {
    VideoCard,
  },
};
</script>
<style lang="scss" scoped>
.router-container {
  padding: 0;
}
</style>
